package cn.xiaok.springboot.controller;

import cn.xiaok.springboot.entity.ExcelEntity;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xueqian.shi
 * @date 2020/9/22
 */
@RestController
@RequestMapping("/excel")
public class EasyExcelController {

    @RequestMapping("/write")
    public String wirte() {

        StringBuffer fileName = new StringBuffer();
        fileName.append("D:\\xiaok");
        fileName.append(System.currentTimeMillis());
        fileName.append(".xlsx");

//        String fileName = "D:\\签子" + System.currentTimeMillis() + ".xlsx";

        EasyExcel.write(fileName.toString(), ExcelEntity.class)
                .sheet("s1")
                .doWrite(createDemo());

        return "ok";
    }

    /**
     * 造点数据
     * @return
     */
    private List<ExcelEntity> createDemo() {
        List list = new ArrayList();

        for (int i = 0; i < 10; i++) {
            ExcelEntity e = new ExcelEntity();
            e.setId(i);
            e.setName(i + "的姓名");
            list.add(e);
        }

        return list;

    }


}
